<?php

namespace app\controllers;


use Yii;
use yii\base\Exception;
use yii\data\ArrayDataProvider;
use yii\helpers\FileHelper;
use yii\helpers\Url;
use yii\web\MethodNotAllowedHttpException;
use yii\web\NotFoundHttpException;
use yii\web\UploadedFile;
use zonday\weixin\api;

class MediaController extends BaseController
{
    public $enableCsrfValidation = false;

    public function actionIndex($type = 'image', $offset = 0, $count = 20)
    {
        $materialApi = new api\Material();
        $result = $materialApi->batchGetMaterial($type, $offset, $count);
        $total = $result['total_count'];
        $count = $result['item_count'];
        $items = $result['item'];
        $countList = $materialApi->getMaterialCount();
        $typeList = [
            'image' => '图片' . $countList['image_count'],
            'video' => '视频' . $countList['video_count'],
            'voice' => '声音' . $countList['voice_count'],
            'news' => '图文' . $countList['news_count'],
        ];

        return $this->render('index', [
            'type' => $type,
            'total' => $total,
            'count' => $count,
            'items' => $items,
            'typeList' => $typeList,
        ]);
    }

    public function actionAddMaterial()
    {
        $materialApi = new api\Material();
        $request = Yii::$app->getRequest();
        $type = $request->post('type', 'image');
        if ($request->isPost && $uploadedFile = UploadedFile::getInstanceByName('media')) {
            $uploadDir = FileHelper::normalizePath(Yii::getAlias('@runtime/uploads'));
            if (!FileHelper::createDirectory($uploadDir)) {
                throw new Exception('上传目录不能新建');
            }
            $filePath = $uploadDir . '/' . time() . '.' . $uploadedFile->getExtension();
            $uploadedFile->saveAs($filePath);
            $materialApi->addMaterial($type, $filePath);
            Yii::$app->getSession()->setFlash('success', '上传永久素材成功');
        }

        $typeList = [
            'image' => '图片',
            'video' => '视频',
            'voice' => '声音',
            'thumb' => '缩略图',
        ];

        return $this->render('addMaterial', [
            'typeList' => $typeList,
            'type' => $type,
            'url' => $materialApi->getFormUrl(),
        ]);
    }

    public function actionGet($mediaId, $type='image')
    {
        $mediaApi = new api\Media();
        $this->redirect($mediaApi->downloadUrl($mediaId));
    }

    public function actionUpload()
    {
        $mediaApi = new api\Media();
        $request = Yii::$app->getRequest();
        $type = $request->post('type', 'image');
        if ($request->isPost && $uploadedFile = UploadedFile::getInstanceByName('media')) {
            $uploadDir = FileHelper::normalizePath(Yii::getAlias('@runtime/uploads'));
            if (!FileHelper::createDirectory($uploadDir)) {
                throw new Exception('上传目录不能新建');
            }
            $filePath = $uploadDir . '/' . time() . '.' . $uploadedFile->getExtension();
            $uploadedFile->saveAs($filePath);
            $result = $mediaApi->upload($type, $filePath);
            return $this->redirect(['get', 'mediaId' => $result['media_id']]);
        }

        $typeList = [
            'image' => '图片',
            'video' => '视频',
            'voice' => '声音',
            'thumb' => '缩略图',
        ];

        return $this->render('addMaterial', [
            'typeList' => $typeList,
            'type' => $type,
            'url' => $mediaApi->getFormUrl($type),
        ]);
    }
}